#SingleInstance force
#include httpQuery.ahk
#include AutoComplete.ahk
#include uriencode.ahk
Autotrim, off


IfExist, settings.ini
{
	Iniread, others, settings.ini, Settings, others
	Iniread, last, settings.ini, Settings, last
}
Else
{
	IniWrite, 0, settings.ini, Settings, others
	IniWrite, 0, settings.ini, Settings, last
	reload
}


;Gui, Show, yCenter xCenter W472 H70,Googlewin
GuiActive=0
Menu,Tray,NoStandard
Menu,Tray,DeleteAll

;Menu,Tray,Add,config,config

Menu,Tray,Add
Menu,Tray,Add,E&xit,CleanUp

Menu,Tray,Default
Return


combo:

	settimer, auto, -500

return

auto:

	gui, submit, nohide
    if (combo=oldcombo)
		return
		
	scombo:=combo
	If combo is not space
	{
		
		cvs:=AutoComplete(combo)
		stringreplace, pipe, cvs, `,, |, All

		guicontrol, , ComboBox1, |%pipe%
		inputRest:=cmboxfill(pipe, combo)
		
	}
	
	len:=StrLen(InputRest)
	oldcombo=%combo%%InputRest%
	if not InputRest
		return
	GuiControl, ChooseString, ComboBox1, %oldcombo%

	sendinput, {right}{shift down}{left %len%}{shift up}
	
	
return



cmboxfill(haystack, needle){
		If (getkeystate("delete")  OR getkeystate("backspace"))
			Return
		re =\|\Q%needle%\E(?P<Rest>.*?)(\||$)
		regexmatch(haystack, re,Input)
		len := StrLen(InputRest)

		return InputRest
	}


~esc::
	{
		gosub GuiEscape2
	}
return

Ctrl::
	If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < 500)
	{
		If GuiActive
		{
			GoSub, GuiEscape2
		}
		Else
		{

			Gui, Color, 000000
			gui,add,Picture,x0 y-5, back.png
			;Gui, Font, S10 CBlue,EuroseWide Heavy
			;gui,add,text,cBlue,Quick Google

			Gui, +LastFound +ToolWindow +alwaysontop
			WinSet, Transparent, 50
			WinSet, Region, 5-5 W460 H70 R10-10
			Gui, -Caption
			Gui, Font, S22 Default Cblack, EuroseWide Heavy
			;Gui, Add, Edit, x10 y25 w450 h40

			Gui, Add, ComboBox,+wanttab r5 gcombo vcombo +vscroll x10 y10 w450 h40,%histroy%
			Gui, Add, Button, w0 h0 Default, &Open
			Gui, Show, yCenter xCenter W472 H100, Googlewin

			GuiActive = 1

			FadeIn(250, "Googlewin")
			KeyWait, RCTRL
		}
	}
Return

ButtonOpen:
	{
		ControlGetText, search, Edit1,Googlewin
		histroy=%histroy%|%search%
		IniWrite, %histroy%, settings.ini, Settings, histroy
		google = http://www.google.com/search?q=%search%
		run %google%
		GoSub, GuiEscape2
	}


~lbutton::
	{
		IfWinActive Googlewin
		{
			return
		}
		Else
		{
			If GuiActive
			{
				gosub GuiEscape2
				gui,destroy
			}
		}
	}
return


FadeIn(GUI_TRANS, WinName)
	{
		transL := 0
		Loop, 10
		{
			transL += GUI_TRANS//10
			WinSet, Transparent, %transL%, %WinName%
			Sleep, 10
		}
	}
FadeOut(GUI_TRANS, WinName)
	{
		transL := GUI_TRANS
		Loop, 10
		{
			transL -= GUI_TRANS//10
			WinSet, Transparent, %transL%, %WinName%
			Sleep, 10
		}
		gui,destroy
	}



Return

config:
	{
		Gui 2: Add, Text, x16 y6 w130 h20 , Show other search options:
		If (others=1)
		{
			Gui 2: Add, Radio, x156 y6 w40 h20 checked vother, Yes
			Gui 2: Add, Radio, x196 y6 w40 h20 vnoother, No
		}
		Else
		{
			Gui 2: Add, Radio, x156 y6 w40 h20 vother, Yes
			Gui 2: Add, Radio, x196 y6 w40 h20 checked vnoother, No
		}
		Gui 2: Add, Button, x156 y36 w80 h20 gsave, Save
		Gui 2: Show, x135 y95 h64 w238, Config
	}
return

save:
	{
		gui 2:submit
		IniWrite, %other%, settings.ini, Settings, others
	}
reload

GuiEscape2:
	GuiActive = 0
	FadeOut(250, "Googlewin")
	ControlSetText, Edit1, ,Googlewin
	Gui, Hide
	gui,destroy
Return

clear:
	{
		IniWrite, "", settings.ini, Settings, histroy
		histroy=
	}
return

CleanUp:
	gui,submit
	IniWrite, %last%, settings.ini, Settings, last
	ExitApp
	
~up::	
~down::
		IfWinActive Googlewin
		{
				blen:=Strlen(scombo)
			sendinput, {home}{right %blen%}{shift down}{end}{shift up}
				gui, submit, nohide
				oldcombo:=combo
		}

return